package com.example.navigationbardemo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.graphics.drawable.VectorDrawableCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import static android.view.KeyEvent.KEYCODE_BACK;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvNavigationOne, tvNavigationTwo, tvNavigationThree;
    private CustomViewPager viewpager_launch;
    private List<Fragment> list_fragment = new ArrayList<>();
    //导航栏img灰色
    private int[] iconArr = {R.drawable.ic_navigation_home, R.drawable.ic_navigation_schedule, R.drawable.ic_navigation_my};
    private BottomViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.StatusBarLightMode(this); //设置白底黑字
        setContentView(R.layout.activity_main);
        initViews();
    }

    public void initViews() {
        tvNavigationOne = findViewById(R.id.tv_navigation_one);
        tvNavigationTwo = findViewById(R.id.tv_navigation_two);
        tvNavigationThree = findViewById(R.id.tv_navigation_three);
        viewpager_launch = findViewById(R.id.viewpager_launch);
        initData();
        initListener();
    }



    public void initData() {
        list_fragment.add(HomeFragment.getInstance());
        list_fragment.add(ScheduleFragment.getInstance());
        list_fragment.add(MyFragment.getInstance());
        initAdapter();
    }

    public void initAdapter() {
        adapter = new BottomViewAdapter(getSupportFragmentManager(), list_fragment);
        viewpager_launch.setAdapter(adapter);
        viewpager_launch.setOffscreenPageLimit(3);//设置缓存view 的个数
    }

    public void initListener() {
        tvNavigationOne.setOnClickListener(this);
        tvNavigationTwo.setOnClickListener(this);
        tvNavigationThree.setOnClickListener(this);
        //BottomNavigationView 添加滑动事件
        viewpager_launch.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                jumpColors(viewpager_launch.getCurrentItem());

            }

            @Override
            public void onPageSelected(int state) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
//                    if (state == 2) {
//                        jumpColors(viewpager_launch.getCurrentItem());
//                    }
            }
        });

    }

    @Override
    public void onClick(View v) {
        jumpColors(v.getId());
    }

    /**
     * 底部导航栏 选中状态
     *
     * @param page
     */
    public void jumpColors(int page) {
        switch (page) {
            case R.id.tv_navigation_one:
            case 0:
                SelectState(tvNavigationOne, iconArr[0], R.color.colorAccent, 0);
                break;
            case R.id.tv_navigation_two:
            case 1:

                SelectState(tvNavigationTwo, iconArr[1], R.color.colorAccent, 1);
                break;
            case R.id.tv_navigation_three:
            case 2:

                SelectState(tvNavigationThree, iconArr[2], R.color.colorAccent, 2);
                break;
        }
    }


    /**
     * 底部导航选中状态
     */
    private void SelectState(TextView view, @DrawableRes int resId, int colour, int item) {
        setGrey();//设置图标选装状态前吧所有先设置成灰色
        //把传过来的textview的图标重新渲染 和文字颜色重新设置
        view.setCompoundDrawablesWithIntrinsicBounds(null, changeVectorgraphColor(resId, colour), null, null);
        view.setTextColor(ContextCompat.getColor(this, colour));
        viewpager_launch.setCurrentItem(item);//切换到viewpage当前选中的页面
    }

    /**
     * 导航栏置灰
     */
    public void setGrey() {
        tvNavigationOne.setCompoundDrawablesWithIntrinsicBounds(null, changeVectorgraphColor(iconArr[0], R.color.colorBlack), null, null);
        tvNavigationOne.setTextColor(ContextCompat.getColor(this, R.color.colorBlack));
        tvNavigationTwo.setCompoundDrawablesWithIntrinsicBounds(null, changeVectorgraphColor(iconArr[1], R.color.colorBlack), null, null);
        tvNavigationTwo.setTextColor(ContextCompat.getColor(this, R.color.colorBlack));
        tvNavigationThree.setCompoundDrawablesWithIntrinsicBounds(null, changeVectorgraphColor(iconArr[2], R.color.colorBlack), null, null);
        tvNavigationThree.setTextColor(ContextCompat.getColor(this, R.color.colorBlack));
    }

    /**
     * 改变矢量图形的颜色
     *
     * @param resId
     * @param colour
     * @return
     */
    public Drawable changeVectorgraphColor(@DrawableRes int resId, int colour) {
        VectorDrawableCompat vectorDrawableCompat4 = VectorDrawableCompat.create(getResources(), resId, getTheme());
        vectorDrawableCompat4.setTint(getResources().getColor(colour));//你需要改变的颜色
        return vectorDrawableCompat4;
    }

    /**
     * 首页适配器
     */
    public class BottomViewAdapter extends FragmentPagerAdapter {
        private List<Fragment> mFragmentList;

        public BottomViewAdapter(FragmentManager manager, List<Fragment> mFragmentList) {
            super(manager);
            this.mFragmentList = mFragmentList;
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }
    }

    /**
     * 菜单、返回键响应
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KEYCODE_BACK) {
            ExitProgram.getInstance().exitBy2Click(this, 1); //调用双击退出函数
        }
        return false;
    }
}
